//
//  collectionviewtwo.m
//  BAOASystem
//
//  Created by michael on 2018/1/26.
//  Copyright © 2018年 czd. All rights reserved.
//
#import "CollectionViewCelltwo.h"
#import "collectionviewtwo.h"
@interface collectionviewtwo ()<UICollectionViewDelegate,UICollectionViewDataSource> {
    
    //UI相关
    
    UIPageControl *_pageControl;
    
    NSTimer *_timer;
    
    //数据
    NSMutableArray *_titles;
    
}
@end
@implementation collectionviewtwo
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self buildUI];
    }
    return self;
}
- (void)buildUI {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(KScreenWidth-20, self.bounds.size.height-30);
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 10, KScreenWidth-20, self.bounds.size.height-30) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = true;
    _collectionView.backgroundColor = [UIColor clearColor];
//    [_collectionView registerClass:[XLCycleCell class] forCellWithReuseIdentifier:@"XLCycleCell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCelltwo" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCelltwo"];

    _collectionView.showsHorizontalScrollIndicator = false;
    [self addSubview:_collectionView];
    
    CGFloat controlHeight = 20.0f;
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - controlHeight, self.bounds.size.width, controlHeight)];
    _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
//        [_pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];  //用户点击UIPageControl的响应函数

    [self addSubview:_pageControl];
    
//    _timer = [NSTimer scheduledTimerWithTimeInterval:ScrollInterval target:self selector:@selector(showNext) userInfo:nil repeats:true];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _data.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString* cellId = @"CollectionViewCelltwo";
    CollectionViewCelltwo *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    cell.dic = _data[indexPath.row];
            [cell layoutSubviews];

    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

NSDictionary *dic =_data[indexPath.row];
shangpinxqViewController *vc = [[shangpinxqViewController alloc]init];
vc.pdid = dic[@"id"];
vc.hidesBottomBarWhenPushed = YES;
[self.ViewController.navigationController pushViewController:vc animated:YES];
NSLog(@"111");
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self cycleScroll];
    //拖拽动作后间隔3s继续轮播
//    [_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:ScrollInterval]];
}
- (void)cycleScroll {
    NSInteger page = _collectionView.contentOffset.x/_collectionView.bounds.size.width;
    if (page == 0) {//滚动到左边
//        _collectionView.contentOffset = CGPointMake(_collectionView.bounds.size.width * (_data.count - 2), 0);
//                _collectionView.contentOffset = CGPointMake(_collectionView.bounds.size.width * (_data.count ), 0);

//        _pageControl.currentPage = _data.count - 2;
                _pageControl.currentPage = 0 ;

    }else if (page == _data.count - 1){//滚动到右边
//        }else if (page == _data.count ){//滚动到右边

//        _collectionView.contentOffset = CGPointMake(_collectionView.bounds.size.width, 0);
        _pageControl.currentPage = page;
    }else{
        _pageControl.currentPage = page ;
//                _pageControl.currentPage = page ;

    }
}
- (void)pageTurn:(UIPageControl*)sender
{
    //令UIScrollView做出相应的滑动显示
    CGSize viewSize = _collectionView.frame.size;
    CGRect rect = CGRectMake(sender.currentPage * viewSize.width, 0, viewSize.width, viewSize.height);
    [_collectionView scrollRectToVisible:rect animated:YES];
}
- (void)setData:(NSArray<NSDictionary *> *)data {
_data = data;
//    _titles = [NSMutableArray arrayWithArray:data];
//    [_data addObject:data.firstObject];
//    [_data insertObject:data.lastObject atIndex:0];
//    [_collectionView setContentOffset:CGPointMake(_collectionView.bounds.size.width, 0)];
    _pageControl.numberOfPages = data.count;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
